/*
 *   Copyright 2008 Marcos Silva Pereira
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 */
class ProfessorTests extends GroovyTestCase {

    void test_should_save_when_all_attributes_are_ok() {

        Professor professor = new Professor(name:"The Ok Professor", email:'the.ok.professor@email.com', site:'http://ok.professor.com', phone:'4444-4444')
        professor.save()

        assertNotNull professor.id
    }

    void test_should_save_when_optionals_are_not_setted() {

        Professor professor = new Professor(name:"The Ok Professor", email:'with.requerid.only@email.com')
        professor.save()

        assertNotNull professor.id
    }

    void test_validate_should_fail_when_name_is_null() {

        def message = 'Should be invalid when name is null'
        mustBeInvalid message, [email:'professor@email.com', site:'http://site.com']
        
    }

    void test_validate_should_fail_when_name_is_empty() {

        def message = 'Should be invalid when name is an empty string'
        
        mustBeInvalid message, [email:'prof@email.com', site:'http://site.com', name:'']
        mustBeInvalid message, [email:'prof@email.com', site:'http://site.com', name:'    ']

    }

    void test_validate_should_fail_when_email_is_null() {

        String message = 'Should be invalid when email is null'
        mustBeInvalid message, [name:'The Professor', site:'http://site.com']

    }

    void test_validate_should_fail_when_email_is_empty() {

        String message = 'Should be invalid when email is an empty string'
        mustBeInvalid message, [name:'The Professor', site:'http://site.com', email:'']
        mustBeInvalid message, [name:'The Professor', site:'http://site.com', email:'   ']

    }

    void test_validate_should_fail_when_email_is_not_valid() {

        String message = 'Should be invalid when email have wrong format'
        mustBeInvalid message, [name:'The Professor', site:'http://site.com', email:'wrong']

    }

    void test_should_not_save_when_email_is_repeated() {
        String message = 'Should be invalid when email is already in use'
        Professor professor = new Professor(name:'The Professor', site:'http://site.com', email:'professor1@email.com')
        professor.save()
        assertNull message, professor.id
        assertTrue  professor.hasErrors()
    }

    void test_should_list_all_professors() {
        def professors = Professor.list()
        assertEquals(20, professors.size())
    }

    private void mustBeInvalid(message, params) {

        Professor professor = new Professor()
        professor.properties = params

        assertFalse message, professor.validate()
    }
    
}
